package com.sun.test.controller;


import com.mysql.cj.log.Log;
import com.sun.test.common.ErrorCode;

import com.sun.test.common.Result;
import com.sun.test.execption.BusinessException;
import com.sun.test.model.Users;
import com.sun.test.service.UsersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.logging.Logger;

@Slf4j
@RestController
@RequestMapping("/api")
@Tag(name = "用户") //修饰类，设置类的说明信息
public class UserController {

    @Autowired
    private UsersService usersService;

    /**
     * 查询所有用户
     * @return
     */
    @GetMapping("/users")
    @Operation(summary = "查询所有用户")
    @ApiResponse(responseCode = "200", description = "成功返回用户数据",
            content = @Content(schema = @Schema(implementation = Users.class)))
    public Result<List<Users>> getAllUsers(){

        List<Users> list = usersService.list();
        if (list.isEmpty()){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        log.info("GET-/api/users:{}",list);
        return Result.success("200",list);
    }

    @PostMapping("/users")
    @Operation(summary = "添加用户")
    public Result<ErrorCode> addUser(@RequestBody Users users){
        if (users == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        log.info("参数-POST-/api/users:{}",users);
        boolean save = usersService.save(users);
        if (!save){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"创建用户失败");
        }
        log.info("POST-/api/users:创建用户成功");
        return Result.success(ErrorCode.SUCCESS);
    }

    @GetMapping("/users/{id}")
    @Operation(summary = "根据id查询用户")
    public Result<Users> getUserById(@PathVariable Integer id){
        if (id == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        Users user = usersService.getById(id);
        if (user == null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"未查询到数据");
        }
        return Result.success("200",user);
    }



    @PutMapping("/users")
    @Operation(summary = "修改用户")
    public Result<ErrorCode> updateById(@RequestBody Users updateUsers){
        Integer id = updateUsers.getId();
        if (id == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        Users user = usersService.getById(id);
        if (user == null){
            throw new BusinessException(ErrorCode.PARAMETER_ERROR);
        }
        boolean result = usersService.updateById(updateUsers);
        if (!result){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        return Result.success(ErrorCode.SUCCESS);
    }

    @DeleteMapping("/users/{id}")
    @Operation(summary = "修改用户")
    public Result<ErrorCode> deleteById(@PathVariable Integer id){
        if (id == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        boolean result = usersService.removeById(id);
        if (!result){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        return Result.error(ErrorCode.SUCCESS);
    }
}
